package main

import (
	"fmt"
)

// 学生结构体
type Student struct {
	ID    int
	Name  string
	Age   int
	Major string
}

// (1) 打印所有属性
func (s Student) Print() {
	fmt.Printf("ID: %d, Name: %s, Age: %d, Major: %s\n", s.ID, s.Name, s.Age, s.Major)
}

// (2) 修改任一属性的任一值
func (s *Student) SetAttribute(attr string, value interface{}) error {
	switch attr {
	case "ID":
		if v, ok := value.(int); ok {
			s.ID = v
			return nil
		}
	case "Name":
		if v, ok := value.(string); ok {
			s.Name = v
			return nil
		}
	case "Age":
		if v, ok := value.(int); ok {
			s.Age = v
			return nil
		}
	case "Major":
		if v, ok := value.(string); ok {
			s.Major = v
			return nil
		}
	}
	return fmt.Errorf("invalid attribute: %s", attr)
}
func main() {
	student := Student{ID: 1, Name: "mayibo", Age: 21, Major: "Blockchain"}
	student.Print()
	err := student.SetAttribute("Age", 18)
	if err != nil {
		fmt.Println(err)
	}
	student.Print()
}
